<template>
  <div class="content">
    <div class="address">
      <ul>
        <li v-for="(item, index) in address" v-if="item.isUsing===1">
          <div class="top" @click="click(index)">
            <div class="tTop">
              <div class="name">
                {{item.userName}}
              </div>
              <div class="tel">
                {{item.phoneNumber}}
              </div>
            </div>
            <div class="dizhi">
              <p>{{item.address}}</p>
              <p>{{item.street}}</p>
            </div>
          </div>
          <div class="bottom">
            <div class="select" @click="selected(index)">
              <i :class="item.isUsing===1?'selected':''"></i>
              <b :class="item.isUsing===1?'red':''">设为默认</b>
            </div>
            <div class="operate">
              <div ><img src="../assets/images/ico_bianji@1x.png" alt="" @click="edit(index)">编辑</div>
              <div class="right" @click="remove(index)"><img src="../assets/images/ico_shanchu@1x.png" alt="">删除</div>
            </div>
          </div>
        </li>
        <li v-for="(item, index) in address" v-if="item.isUsing===0">
          <div class="top" @click="click(index)">
            <div class="tTop">
              <div class="name">
                {{item.userName}}
              </div>
              <div class="tel">
                {{item.phoneNumber}}
              </div>
            </div>
            <div class="dizhi">
              <p>{{item.address}}</p>
              <p>{{item.street}}</p>
            </div>
          </div>
          <div class="bottom">
            <div class="select" @click="selected(index)">
              <i :class="item.isUsing===1?'selected':''"></i>
              <b :class="item.isUsing===1?'red':''">设为默认</b>
            </div>
            <div class="operate">
              <div ><img src="../assets/images/ico_bianji@1x.png" alt="" @click="edit(index)">编辑</div>
              <div class="right" @click="remove(index)"><img src="../assets/images/ico_shanchu@1x.png" alt="">删除</div>
            </div>
          </div>
        </li>
      </ul>
      <div class="btn">
        <a href="javascript:;" class="btn-next" @click="NewAddress()">添加新地址</a>
      </div>
    </div>
    <loading v-show="showLoading"></loading>
    <alertTip v-if="showAlert" @closeTip="go()" :alertText="alertText"></alertTip>
  </div>

</template>

<script>
  /* eslint-disable semi,no-undef */
  import wx from 'weixin-js-sdk'
  import { wxHttpTool,wx_config, http_getParam, get_shopQrcoe,wx_share,wx_hideAllMenu } from '../components/httpUtil/httpUtil'
  import {msg_10046} from '../message/msg_10046'
import {relogin} from '../components/httpUtil/httpUtil'
  export default {
    name: '',
    data: function () {
      return {
        address: [],
        showLoading: true, //显示加载动画
        showAlert:false,
        alertText:null
//        select: 'selected'
//        curId: 0
      }
    },
    methods: {
      alertTip(msg){
        this.showAlert = true;
        this.alertText = msg;
      },
      // 隐藏动画
      hideLoading(){
        this.showLoading = false;
      },
      go(){
        this.showAlert = false;
      },
      getData () {
        const jsonValue = JSON.stringify({
          'c': '10025',
          'p': {
            'userId': JSON.parse(window.localStorage.data).id, // 用户ID
//            'userId': 1009, // 用户ID
            'tokenId': token()
          }
        });
        this.$http.post(api + '/qqs.us', jsonValue).then((res) => {
//          console.log(res.data);
          const data = res.data.p;
          console.log(data.address)
          if (data.isTrue === true) {
            this.hideLoading();
            this.address = data.address;
          } else {
            this.hideLoading();
            this.alertTip(data.errorMsg);
            if (data.errorCode === 90) {
              this.go=()=> {
                this.showAlert = false
                let _this =this
                relogin(_this)
              }
            }
          }
        }, (res) => {
          this.hideLoading();
          this.alertTip('网络错误');
          this.go = ()=>{
             this.showAlert = false;
          }
        })
      },
      // 选取收货地址
      click (index) {
//        console.log(index)
        this.hideLoading();
        this.$router.push({name: 'Exchange', params: {addr: this.address[index]}})
      },
      // 设为默认地址
      selected (index) {
        this.showLoading=true;
        console.log(this.address[index].id)
        const jsonValue = JSON.stringify({
          'c': '10024',
          'p': {
            'userId': JSON.parse(window.localStorage.data).id, // 用户ID
            'tokenId': token(),
            'addressId': this.address[index].id  // 地址ID
          }
        });
        this.$http.post(api + '/qqs.us', jsonValue).then((res) => {
          console.log(res);
          this.hideLoading();
          const data = res.data.p;
//          console.log(data.address)
          if (data.isTrue === true) {

            this.address = data.address;
//            console.log(this.address[0].isUsing)
          } else {
            this.alertTip('data.errorMsg');
            if (data.errorCode === 90) {
              this.go= ()=> {
                this.showAlert = false
                let _this = this
                relogin(_this)
              }
            }
          }
        }, (res) => {
          this.hideLoading();
          this.alertTip('网络错误');
          this.go = ()=>{
             this.showAlert = false;
          }
        })
      },
      // 编辑地址
      edit (index) {
//        console.log(this.address[index].id)
        this.$router.push({name: 'addressEdit',
          params: {
            name: this.address[index].userName,
            number: this.address[index].phoneNumber,
            address: this.address[index].address,
            street: this.address[index].street,
            addressId: this.address[index].id
          }
        })
      },
      remove (index) {
        this.showLoading=true;
        console.log(this.address[index].id);
        const jsonValue = JSON.stringify({
          'c': '10032',
          'p': {
            'userId': JSON.parse(window.localStorage.data).id, // 用户ID
            'tokenId': token(),
            'addressId': this.address[index].id  // 地址ID
          }
        });
        this.$http.post(api + '/qqs.us', jsonValue).then((res) => {
          console.log(res);
          const data = res.data.p;
//          console.log(data)
          if (data.isTrue === true) {
            this.hideLoading();
            this.address = data.address;
//            console.log(this.address[0].isUsing)
          } else {
            this.hideLoading();
            this.alertTip(data.errorMsg);
            if (data.errorCode === 90) {
              this.go=()=> {
                this.showAlert = false
                let _this = this
                relogin(_this)
              }
            }
          }
        }, (res) => {
          this.hideLoading();
          this.alertTip('网络错误');
          this.go = ()=>{
             this.showAlert = false;
          }
        })
      },
      NewAddress () {
        this.$router.replace('/addressCreate')
      }
    },
    mounted () {
      // goHistoryUrl();
      // window.addEventListener("popstate", function(e) {
      //   console.log('location: ' + document.location);
      //   console.log('state: ' + JSON.stringify(event.state));
      //   this.$router.replace(window.localStorage.historyUrl)
      // }, false);



//      console.log(this.$route.params.addr)
      if (this.$route.params.addr !== undefined) {
        this.address = this.$route.params.addr;
      } else {
        this.getData();
      }
    },
    created(){
      wxHttpTool.wxAuthor(this, function (varRet, varJson, varThis = this) {
        if (varRet == -1) {
          varThis.alertTip('网络错误');
          varThis.go = function () {
            varThis.showAlert = false;
          }

        }
        // 关注钱应该判断下是否已经关注过了
        msg_10046.send(varThis,msg_10046.entity(),function(varThis,res){
          varThis.wxlist=res.data.p.sign
          wx_config(varThis.wxlist);
          wx_hideAllMenu();
          wx.hideMenuItems({
            //禁用单独分享朋友圈QQ空间等功能
            menuList: ["menuItem:share:timeline","menuItem:share:QZone","menuItem:share:qq"] // 要隐藏的菜单项，所有menu项见附录3
          });
        })


      },this.type)
    },
    updated () {
//      this.getData();
//      this.next()
    }
//    beforeDestroy () {
//      // 保存默认地址
//      window.localStorage.pretermission = JSON.stringify(this.address[this.curId]);
//    }
  }
</script>

<style lang="scss" scoped>
  .content {
    height: 100%;
    .address {
      width: 100%;
      height: 100%;
      box-sizing: border-box;
      background: #f4f4f4;
      ul {
        padding-top: 20px;
        margin-bottom: 120px;
        /*background: #f4f4f4;*/
        li {
          background: #ffffff;
          margin-bottom: 20px;
          .top {
            height: 208px;
            box-sizing: border-box;
            padding-top: 46px;
            font-size: 28px;
            color: #3b3b43;
            border-bottom: 1px solid #dcdcdc;
            .tTop{
              height: 40px;
              .name {
                float: left;
                padding-left: 34px;
              }
              .tel {
                float: right;
                padding-right: 30px;
              }
            }
            .dizhi {
              font-size: 26px;
              color: #a1a1a1;
              /*float: left;*/
              padding-left: 34px;
              margin-top: 30px;
              p{
                word-wrap: break-word;
                //word-break设置强行换行;normal 亚洲语言和非亚洲语言的文本规则，允许在字内换行word-break: normal;
              }
            }
          }
          .bottom {
            position: relative;
            width: 100%;
            height: 90px;
            .select {
              float: left;
              padding-top: 26px;
              padding-left: 30px;
              b{
                font-weight: 400;
              }
              i{
                vertical-align: middle;
                display: inline-block;
                background: url('../assets/images/ico_2@1x.png') no-repeat;
                background-size: 100% 100%;
                width: 32px;
                height: 32px;
                margin-right: 8px;
              }
              .selected{
                background: url('../assets/images/ico_1@1x.png') no-repeat;
                background-size: 100% 100%;
              }
              .red{color: #ff0000}
            }
            .operate{
              width: 240px;
              height: 90px;
              float: right;
              padding-right: 14px;
              .right{
                padding-left: 35px;
              }
              div{
                float: left;
                padding-top: 26px;
                img{
                  vertical-align: middle;
                  width: 26px;
                  height: 26px;
                  padding-right: 12px;
                }
              }
            }
          }
        }
      }
      .btn{
        width:100%;
        height: 120px;
        text-align: center;
        line-height: 120px;
        background: #f4f4f4;
        position: fixed;
        bottom: 0;
        .btn-next{
          display: inline-block;
          width: 580px;
          height: 74px;
          margin:0;
          border-radius: 35px;
          border: 1px solid #fe3519;
          color: #fe3519;
          text-align: center;
          line-height: 74px;
          font-size: 26px;
        }
      }
    }
  }
</style>
